/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taglibs.admin;

import daos.EditorialCategoryDAO;
import daos.EditorialDAO;
import daos.ItemDAO;
import java.util.ArrayList;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import models.Editorial;
import models.EditorialCategory;
import models.Item;
import util.Common;
import util.WebConst;

/**
 *
 * @author trietto
 */
public class EditorialFormEdit extends TagSupport {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /*
     * (non-Javadoc)
     * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
     */
    public int doStartTag() {
        try {
            JspWriter writer = pageContext.getOut();
            String actionName = pageContext.getRequest().getParameter(WebConst.ACTION);

            ArrayList<String> lstEditorialCategoriesData = new ArrayList<String>();
            ArrayList<EditorialCategory> lstEditorialCategories = EditorialCategoryDAO.singleton().selectAll();
            for (EditorialCategory editorialCategory : lstEditorialCategories) {
                lstEditorialCategoriesData.add(editorialCategory.getName() + "\t" + editorialCategory.getEditorialCatId());
            }

            ArrayList<String> lstItemData = new ArrayList<String>();
            ArrayList<Item> lstItem = ItemDAO.singleton().selectAll();
            for (Item item : lstItem) {
                lstItemData.add(item.getName() + "\t" + item.getItemId());
            }

            if (actionName.equalsIgnoreCase(WebConst.EDIT)) {
                int articleId = Integer.parseInt(pageContext.getRequest().getParameter("articleid"));
                Editorial editorial = EditorialDAO.singleton().select(articleId);
                if (editorial == null && actionName.equalsIgnoreCase(WebConst.EDIT)) {
                    return SKIP_BODY;
                }
                writer.println("<form action='AdminEditorialServlet?action=update&articleid=" + articleId + "' method='POST'>");
                writer.println("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
                writer.println("<tr>");
                writer.println("<td>Editorial Category:</td>");
                writer.println("<td>" + Common.createComboBox("cbEditorialCategory", "cbEditorialCategory", (String[]) (lstEditorialCategoriesData.toArray(new String[0])), String.valueOf(editorial.getEditorialCatId()), "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>Item:</td>");
                writer.println("<td>" + Common.createComboBox("cbItem", "cbItem", (String[]) (lstItemData.toArray(new String[0])), String.valueOf(editorial.getItemId()), "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>Article Title:</td>");
                writer.println("<td>" + Common.createTextbox("txtArticleTitle", "txtArticleTitle", editorial.getArticleTitle(), "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>Article Description:</td>");
                writer.println("<td>" + Common.createTextbox("txtArticleDesc", "txtArticleDesc", editorial.getArticleDesc(), "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr colspan='2'>");
                writer.println("<td>" + Common.createSubmitButton("btnUpdate", "btnUpdate", "Update", "") + "</td>");
                writer.println("</tr>");
                writer.println("</table>");
                writer.println("</form>");
            } else if (actionName.equalsIgnoreCase(WebConst.NEW)) {
                writer.println("<form action='AdminEditorialServlet?action=create' method='POST'>");
                writer.println("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
                writer.println("<tr>");
                writer.println("<td>Editorial Category:</td>");
                writer.println("<td>" + Common.createComboBox("cbEditorialCategory", "cbEditorialCategory", (String[]) (lstEditorialCategoriesData.toArray(new String[0])), "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>Item:</td>");
                writer.println("<td>" + Common.createComboBox("cbItem", "cbItem", (String[]) (lstItemData.toArray(new String[0])), "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>Article Title:</td>");
                writer.println("<td>" + Common.createTextbox("txtArticleTitle", "txtArticleTitle", "", "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr>");
                writer.println("<td>Article Description:</td>");
                writer.println("<td>" + Common.createTextbox("txtArticleDesc", "txtArticleDesc", "", "", "") + "</td>");
                writer.println("</tr>");
                writer.println("<tr colspan='2'>");
                writer.println("<td>" + Common.createSubmitButton("btnCreate", "btnCreate", "Create", "") + "</td>");
                writer.println("</tr>");
                writer.println("</table>");
                writer.println("</form>");
            }

        } catch (Exception e) {
        }
        return SKIP_BODY;
    }
}
